package com.aduerr.v1shouyin.mvp.base;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.view.*;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

import androidx.fragment.app.FragmentActivity;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/12.
 * 基础popupwindow弹窗
 */

public class BasePopupWindow extends PopupWindow {
    /**
     * Special value for the height or width requested by a View.
     * MATCH_PARENT means that the view wants to be as big as its parent,
     * minus the parent's padding, if any. Introduced in API Level 8.
     */
    public static final int MATCH_PARENT = -1;

    /**
     * Special value for the height or width requested by a View.
     * WRAP_CONTENT means that the view wants to be just large enough to fit
     * its own internal content, taking its own padding into account.
     */
    public static final int WRAP_CONTENT = -2;
    public FragmentActivity mActivity;
    public LayoutInflater mInflater;
    private float mShowAlpha = 0.68f;
    private View contentView;

    public BasePopupWindow(FragmentActivity mActivity) {
        super(mActivity);
        this.mActivity = mActivity;
        this.mInflater = mActivity.getLayoutInflater();
    }

    public void setContentView(int resource) {
        setContentView(resource, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                true);
    }


    public void setContentView(int resource, int width, int height, boolean focusable) {
        contentView = mInflater.inflate(resource, null);
        setContentView(contentView);
        setWidth(width);
        setHeight(height);
        ButterKnife.bind(this, contentView);
        setBackgroundDrawable(new BitmapDrawable(mActivity.getResources(),
                (Bitmap) null));
//                setBackgroundDrawable(new ColorDrawable(0x000000));
        setFocusable(focusable);
//        setTouchable(focusable);
        setOutsideTouchable(focusable);
        update();
        addKeyListener(contentView);

    }

    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
        showAnimator().start();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        dismissAnimator().start();
    }

    /**
     * 窗口显示，窗口背景透明度渐变动画
     */
    private ValueAnimator showAnimator() {
        ValueAnimator animator = ValueAnimator.ofFloat(1.0f, mShowAlpha);
        animator.addUpdateListener(animation -> {
            float alpha = (float) animation.getAnimatedValue();
            setWindowBackgroundAlpha(alpha);
        });
        animator.setDuration(360);
        return animator;
    }

    /**
     * 窗口隐藏，窗口背景透明度渐变动画
     */
    private ValueAnimator dismissAnimator() {
        ValueAnimator animator = ValueAnimator.ofFloat(mShowAlpha, 1.0f);
        animator.addUpdateListener(animation -> {
            float alpha = (float) animation.getAnimatedValue();
            setWindowBackgroundAlpha(alpha);
        });
        animator.setDuration(320);
        return animator;
    }

    /**
     * 为窗体添加outside点击事件
     */
    private void addKeyListener(View contentView) {
        if(contentView != null) {
            contentView.setFocusable(true);
            contentView.setFocusableInTouchMode(true);
            contentView.setOnKeyListener((view, keyCode, event) -> {
                switch(keyCode) {
                    case KeyEvent.KEYCODE_BACK:
                        dismiss();
                        return true;
                    default:
                        break;
                }
                return false;
            });
            contentView.setOnTouchListener((v, event) -> {
//                    Log.i("TAG", "event.getRawY ===" + event.getRawY() + "v.getY()
//                    ===" + v.getY());
                if(event.getRawY() < 1390) {
                    dismiss();
                }
                return false;
            });

        }
    }

    /**
     * 控制窗口背景的不透明度
     */
    private void setWindowBackgroundAlpha(float alpha) {
        Window window = ((Activity) mActivity).getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.alpha = alpha;
        window.setAttributes(layoutParams);
    }


}
